package com.tongfusoft.document.web.controller.administration;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.Docs;
import com.tongfusoft.document.entity.RoleHasPrivileges;
import com.tongfusoft.document.entity.RoleHasSecurities;
import com.tongfusoft.document.entity.Roles;
import com.tongfusoft.document.entity.SecurityGroup;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.role.service.IDocumentRoleService;
import com.tongfusoft.document.manager.security.service.IDocumentSecurityGroupService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;
import com.tongfusoft.utils.UIDGeneratorService;

@RequestMapping("/manager")
@Controller
public class SecurityController extends BaseController{
	
		@Autowired
		@Qualifier(value="documentSecurityGroupService")
		public IDocumentSecurityGroupService groupService;

		@UrlDesc(value = "安全组管理页")
		@RequestMapping("/securityGroup")
		public String security(HttpServletRequest request, Model model) {
	
			model.addAttribute("mainPageKey", "securityGroup");
			model.addAttribute("mainPage", "manager/securityGroup/securityGroup");
	
			return "base";
		}
		
		

		@UrlDesc(value = "安全组信息列表（分页）")
		@RequestMapping("/group/query")
		@ResponseBody
		public DataTable<SecurityGroup> groupQuery(Model model, int length, int start, int draw, String groupCode,
				String order ){
			DataTable rm = new DataTable();
			try {
				SecurityGroup group = new SecurityGroup();
				int pageIndex = (start / length);
				PageInfo pageInfo = this.groupService.groupPageTable(pageIndex, length, group);
				rm.setDraw(draw);
				rm.setRecordsFiltered(pageInfo.getTotal());
				rm.setRecordsTotal(pageInfo.getTotal());
				rm.setData(pageInfo.getList());
			}catch(Exception e){
				e.printStackTrace();
			}
			
			return rm;
		}

		@UrlDesc(value = "角色安全组对应权限")
		@RequestMapping("/power/query")
		@ResponseBody
		public DataTable<RoleHasSecurities> powerQuery(Model model, int length, int start, int draw, String groupId,
				String order ){
			DataTable rm = new DataTable();
			try {
				RoleHasSecurities roleHasSecurities = new RoleHasSecurities();
				roleHasSecurities.setGroupId(groupId);
				int pageIndex = (start / length);
				PageInfo pageInfo = this.groupService.roleHasSecuritiesPageTable(pageIndex, length, roleHasSecurities);
				
				rm.setDraw(draw);
				rm.setRecordsFiltered(pageInfo.getTotal());
				rm.setRecordsTotal(pageInfo.getTotal());
				rm.setData(pageInfo.getList());
			}catch(Exception e){
				e.printStackTrace();
			}
					
				return rm;
			}

		@UrlDesc(value = "添加安全组")
		@RequestMapping("/group/add")
		@ResponseBody
		public ResultModel groupAdd(SecurityGroup group,RoleHasSecurities roleHasSecurities){
			ResultModel rm = new ResultModel();
			rm.setState("success");
			try{
				this.groupService.createSecurityGroup(group, (Users)super.getUser());
			} catch(ServiceException se){
				rm.setCode(se.getMessage());
				rm.setState("error");
			}catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}
			
			return rm;
		}
		
		@UrlDesc(value = "安全组是否被文档使用")
		@RequestMapping("/isGroupDoc")
		@ResponseBody
		public ResultModel isGroupDoc(String groupId,Model model){
			ResultModel rm = new ResultModel();
			rm.setState("success");

			try {
				List<Docs> list = this.groupService.isGroupByDoc(groupId);
				rm.setData(list);
			} catch (ServiceException se) {
				rm.setCode(se.getMessage());
				rm.setState("error");
			} catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}
			return rm;
		}
		

		@UrlDesc(value = "删除安全组")
		@RequestMapping("/group/del")
		@ResponseBody
		public ResultModel groupDel(SecurityGroup group,RoleHasSecurities roleHasSecurities) {

			ResultModel rm = new ResultModel();
			rm.setState("success");

			try {
				this.groupService.deleteSecurityGroup(group,(Users)super.getUser());
			} catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}

			return rm;

		}

		@UrlDesc(value = "安全组详情")
		@RequestMapping("/group/detail")
		@ResponseBody
		public ResultModel groupDetail(SecurityGroup group) {

			ResultModel rm = new ResultModel();
			rm.setState("success");

			try {
				rm.setData(this.groupService.getSecurityGroup(group));
			} catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}

			return rm;

		}

		@UrlDesc(value = "编辑安全组")
		@RequestMapping("/group/edit")
		@ResponseBody
		public ResultModel groupEidt(SecurityGroup group) {

			ResultModel rm = new ResultModel();
			rm.setState("success");

			try {
				this.groupService.updateSecurityGroup(group, (Users)super.getUser());
			}   catch(ServiceException se){
				rm.setCode(se.getMessage());
				rm.setState("error");
			}catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}

			return rm;

		}


		@UrlDesc(value = "权限详情")
		@RequestMapping("/powerRole/detail")
		@ResponseBody
		public ResultModel powerDetail(RoleHasSecurities roleHasSecurities,String rpId){
			ResultModel rm = new ResultModel();
			rm.setState("success");

			try {
				rm.setData(this.groupService.powerByRpId(rpId));
			} catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}
			return rm;
		}
		
		@UrlDesc(value = "编辑权限")
		@RequestMapping("/powerRole/edit")
		@ResponseBody
		public ResultModel powerEdit(RoleHasSecurities roleHasSecurities){
			ResultModel rm = new ResultModel();
			rm.setState("success");

			try {
				this.groupService.updateRoleHasGroup(roleHasSecurities,(Users)super.getUser());
			}  catch(ServiceException se){
				rm.setCode(se.getMessage());
				rm.setState("error");
			}catch (Exception e) {
				rm.setState("error");
				e.printStackTrace();
			}
			return rm;
		}
}
